package cc.chenwenxi.mypi.autoconfig;

import com.google.common.collect.Lists;
import jodd.io.FileUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.mapdb.DB;
import org.mapdb.DBMaker;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

@Lazy
@Slf4j
@Configuration
@RequiredArgsConstructor
public class PiCnf {

    private final SpringConfig springConfig;
    public final static List<String> 评论s = Lists.newArrayList();

    @Bean
    public DB mapDb() throws IOException {
        String[] strings = FileUtil.readLines(new File(springConfig.getDataPath(), "评论.txt"), StandardCharsets.UTF_8);
        评论s.addAll(List.of(strings));
        log.info("---加载评论数:{}",评论s.size());

        DBMaker.Maker maker = DBMaker.fileDB(new File(springConfig.getDataPath(),"myPiMap.db"))
                .fileMmapEnable()
//                .transactionEnable()
                .closeOnJvmShutdown() //JVM关闭时关闭db
                .checksumHeaderBypass();
        return maker.make();
    }
}
